#region 程序集引用
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Zhang.AdminPage;

#endregion

namespace Zhang.Admin.Areas.UCenter.Label
{
    public class LabelController : BaseController
    {
        #region 新增
        public ActionResult Add()
        {
            ModelName = "标签";
            
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(Zhang.Model.UCenter.Label model)
        {
            IsEdit = false;
            ModelName = "标签";
            
            new Zhang.BLL.UCenter.Label.Label().Insert(model);
            MessageContent = "恭喜您，操作成功";
            const Zhang.Model.MessageType msgType = Zhang.Model.MessageType.Success;
            ShowMessage(msgType);
            return null;
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddAjax(Zhang.Model.UCenter.Label model)
        {
            IsEdit = false;
            ModelName = "标签";

            new Zhang.BLL.UCenter.Label.Label().Insert(model);
            MessageContent = "恭喜您，操作成功";
            const Zhang.Model.MessageType msgType = Zhang.Model.MessageType.Success;
            ShowMessage(msgType);
            return null;
        }
        #endregion

        #region 编辑
        public ActionResult Edit(int id)
        {
            ModelName = "标签";
            
            var model = new Zhang.BLL.UCenter.Label.Label().Select(id);

            if (model != null) return View(model);
            const Zhang.Model.MessageType msgType = Zhang.Model.MessageType.Attention;
            MessageContent = "当前记录不存在（或是已删除）";
            ShowMessage(msgType);
            return null;
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Zhang.Model.UCenter.Label model)
        {
            Zhang.Model.MessageType msgType;

            IsEdit = true;
            ModelName = "标签";
            
            var isSuccess = new Zhang.BLL.UCenter.Label.Label().Update(model);

            if (isSuccess)
            {
                MessageContent = "恭喜您，操作成功";
                msgType = Zhang.Model.MessageType.Success;
            }
            else
            {
                MessageContent = "对不起，操作失败，请重试";
                msgType = Zhang.Model.MessageType.Failure;
            }
            ShowMessage(msgType);
            return null;
        }
        #endregion

        #region 删除
        public ActionResult BatchDelete(string idList)
        {
            Zhang.Model.MessageType msgType;
            ModelName = "标签";
          
            if (!string.IsNullOrEmpty(idList))
            {
                var isSuccess = new Zhang.BLL.UCenter.Label.Label().DeleteMuch(idList.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)) > 0;

                if (isSuccess)
                {
                  
                    MessageContent = "恭喜您，批量删除成功";
                    msgType = Zhang.Model.MessageType.Success;
                }
                else
                {
                   
                    MessageContent = "批量册除操作失败";
                    msgType = Zhang.Model.MessageType.Failure;
                }
            }
            else
            {
                MessageContent = "请选择要删除的记录";
                msgType =Zhang. Model.MessageType.Attention;
            }

            ListSetUrlLink();
            ShowMessage(msgType);
            return null;
        }

        public ActionResult Delete(int id)
        {
            Zhang.Model.MessageType msgType;
            ModelName = "标签";
            
            var isSuccess = new Zhang.BLL.UCenter.Label.Label().Delete(id) > 0;

            if (isSuccess)
            {
                MessageContent = "恭喜您，删除成功";
                msgType = Zhang.Model.MessageType.Success;
            }
            else
            {
                MessageContent = "操作失败";
                msgType = Zhang.Model.MessageType.Failure;
            }

            ListSetUrlLink();
            ShowMessage(msgType);
            return null;
        }
        
        private void ListSetUrlLink()
        {
            DefaultBackLink = new Zhang.Model.Link {Title = "返回列表页", URL = "/UCenter/Label/Label?" + Request["backUrlParam"]};
        }
        #endregion

        #region 列表
        public ActionResult Index(Zhang.Model.UCenter.LabelSch schModel)
        {
            ModelName = "标签";
          
            var pgIndex = PageIndex;
            pgIndex = pgIndex < 1 ? 1 : pgIndex;
            var (item1, totalRs) = new Zhang.BLL.UCenter.Label.Label().SelectList(pgIndex, PageSize, schModel, Sequence);

            if (totalRs > PageSize)
            {
                ViewBag.SplitHtml = Zhang.Utils.SplitPage.GetHtmlCode("/UCenter/Label/Label", 6, pgIndex, PageSize, totalRs);
            }

            ViewBag.SchModel = schModel;
            ViewBag.UrlParam = UrlParam;
            return View(item1);
        }
        #endregion
        
    }
}
